﻿using System;
using System.Collections.Generic;
using System.Data;
using Components;
using ServiceHelper.Util;

namespace ServiceHelper.Set
{
    public class ShopSetting
    {
        private Dictionary<string, Dictionary<string, string>> _setting = null;

        private readonly string _shopId = null;

        public ShopSetting(string shopid)
        {
            if (string.IsNullOrEmpty(shopid))
            {
                throw new Exception("门店不应为空");
            }

            _shopId = shopid;
        }

        public void ClearAll()
        {
            _setting = null;
        }

        private void Load()
        {
            _setting = new Dictionary<string, Dictionary<string, string>>();

            var data = CommonManager.Entity.ExecDBFun("getshopsetting",
               QB.Entity.AddParam("comcode", ServerContext.ComCode));

            if (data == null || data.Rows.Count == 0)
                return;

            foreach (DataRow dr in data.Rows)
            {
                var shopid = Validation.ToString(dr["shopid"]);
                var setname = Validation.ToString(dr["setname"]);
                var value = Validation.ToString(dr["value"]);

                Dictionary<string, string> dict = null;
                if (_setting.ContainsKey(shopid))
                {
                    dict = _setting[shopid];
                }
                else
                {
                    dict = new Dictionary<string, string>();
                    _setting.Add(shopid, dict);
                }

                if (dict.ContainsKey(setname))
                    dict[setname] = value;
                else
                    dict.Add(setname, value);
            }
        }

        public string GetValue(string setname)
        {
            if (null == _setting)
                Load();

            if (null == _setting)
                return null;

            if (_setting.ContainsKey(_shopId))
            {
                var dict = _setting[_shopId];

                if (dict != null && dict.ContainsKey(setname))
                    return dict[setname];
            }

            return null;
        }

        public bool DirectStock
        {
            get
            {
                var value = GetValue(Components.Enum.ShopSetting.DirectStock);

                return "Y" == value; //默认不直接发货
            }

        }

        public bool NeedAudit
        {
            get
            {
                var value = GetValue(Components.Enum.ShopSetting.NeedAudit);
                return "N" != value; //默认需要审核
            }
        }

        public bool PrintPrice
        {
            get
            {
                var value = GetValue(Components.Enum.ShopSetting.PrintPrice);

                return "N" != value; //默认需要打印    
            }
        }

        public string ParseJson()
        {
            if (null == _setting)
                Load();

            if (null == _setting)
                return null;

            if (_setting.ContainsKey(_shopId))
            {
                var dict = _setting[_shopId];

                return Json.ToString(dict);
            }

            return null;
        }
    }
}
